<?php

namespace app\api\model;

use app\admin\model\shop\Goods as ShopGoods;
use think\Db;

class Goods extends ShopGoods {
	public function getImageAttr( $value ) {
		return cdnurl( $value, true );
	}

	public function getImagesAttr( $value ) {
		$value = empty( $value ) ? [] : explode( ',', $value );
		foreach ( $value as &$img ) {
			$img = cdnurl( $img, true );
		}
		return $value;
	}

	/**
	 * 推荐商品
	 */
	public function getRecommendList() {
		return $this->with( ['spec', 'category'] )
			->where( ['status' => 1, 'recommend' => 1] )
			->order( ['weigh' => 'desc', 'id' => 'desc'] )
			->limit( 10 )
			->select();
	}

	public function getList( $param, $isPage = false ) {
		$where['status'] = 1;
		if ( !empty( $param['category_id'] ) ) {
			$where['category_id'] = $param['category_id'];
		}
		if ( !empty( $param['name'] ) ) {
			$where['name'] = ['like', '%' . $param['name'] . '%'];
		}
		if ( !empty( $param['ids'] ) ) {
			$where['id'] = ['in', $param['ids']];
		}

		// 商品表名称
		$tableName = $this->getTable();
		// 多规格商品 最高价与最低价
		$GoodsSpec   = new ShopGoodsSpec();
		$minPriceSql = $GoodsSpec->field( ['MIN(goods_price)'] )
			->where( 'goods_id', 'EXP', "= `$tableName`.`id`" )->buildSql();
		$maxPriceSql = $GoodsSpec->field( ['MAX(goods_price)'] )
			->where( 'goods_id', 'EXP', "= `$tableName`.`id`" )->buildSql();

		if ( $isPage ) {
			$data = $this->field( ['*', '(sales_initial + sales_actual) as goods_sales',
				"$minPriceSql AS goods_min_price",
				"$maxPriceSql AS goods_max_price"
			] )->with( ['spec'] )->where( $where )->order( 'weigh', 'desc' )->order( 'id', 'desc' )
				->paginate( 10 )->toArray();
		} else {
			$data = collection( $this->field( ['*', '(sales_initial + sales_actual) as goods_sales',
				"$minPriceSql AS goods_min_price",
				"$maxPriceSql AS goods_max_price"
			] )->with( ['spec'] )->where( $where )->order( 'weigh', 'desc' )->order( 'id', 'desc' )
				->select() )->toArray();
		}
		return $data;
	}

	public static function detail( $goods_id ) {
		$dataout = self::get( $goods_id, ['spec', 'specRel'] );
		return $dataout;
	}

	/**
	 * 商品多规格信息
	 */
	public function getGoodsSku( $goods_sku_id ) {
		$goodsSkuData = array_column( $this['spec'], null, 'spec_sku_id' );
		if ( !isset( $goodsSkuData[$goods_sku_id] ) ) {
			return false;
		}
		$goods_sku = $goodsSkuData[$goods_sku_id];
		// 多规格文字内容
		$goods_sku['goods_attr'] = '';
		$goods_attr              = [];
		if ( $this['spec_type'] === '20' ) {
			$attrs    = explode( '_', $goods_sku['spec_sku_id'] );
			$spec_rel = array_column( $this['specRel'], null, 'id' );

			foreach ( $attrs as $specValueId ) {
				$goods_attr[] = $spec_rel[$specValueId]['spec_value'];
				//$goods_sku['goods_attr'] .= $spec_rel[$specValueId]['spec']['spec_name'] . ':' . $spec_rel[$specValueId]['spec_value'] . '; ';
			}

			//这里格式化 展示图片
			$goods_sku['img_show'] = $goods_sku['spec_image'] == '' ? '' : cdnurl( $goods_sku['spec_image'], true );
		}
		$goods_sku['goods_attr'] = implode( '、', $goods_attr );
		return $goods_sku;
	}

	/**
	 * 商品多规格信息
	 */
	public function getGoodsSkuById( $goods_spec_id ) {
		$goodsSkuData = array_column( $this['spec'], null, 'goods_spec_id' );
		if ( !isset( $goodsSkuData[$goods_spec_id] ) ) {
			return false;
		}
		$goods_sku = $goodsSkuData[$goods_spec_id];
		// 多规格文字内容
		$goods_sku['goods_attr'] = '';
		$goods_attr              = [];
		if ( $this['spec_type'] === '20' ) {
			$attrs    = explode( '_', $goods_sku['spec_sku_id'] );
			$spec_rel = array_column( $this['specRel'], null, 'id' );

			foreach ( $attrs as $specValueId ) {
				$goods_attr[] = $spec_rel[$specValueId]['spec_value'];
				//$goods_sku['goods_attr'] .= $spec_rel[$specValueId]['spec']['spec_name'] . ':' . $spec_rel[$specValueId]['spec_value'] . '; ';
			}

			//这里格式化 展示图片
			$goods_sku['img_show'] = $goods_sku['spec_image'] == '' ? '' : cdnurl( $goods_sku['spec_image'], true );
		}
		$goods_sku['goods_attr'] = implode( '、', $goods_attr );
		return $goods_sku;
	}

	//
	public function getListByIds( $goodsIds ) {
		//$dataout = $this->with(['spec', 'spec_rel.spec'])->where('id', 'in', $goodsIds)->select();
		$dataout = $this->where( 'id', 'in', $goodsIds )->select();
		return $dataout;
	}

	public function getListById( $goodsId ) {
		$dataout = $this->with( ['spec', 'spec_rel.spec'] )->where( 'id', 'eq', $goodsId )->find();
		return $dataout;
	}

	public function updateStockSales( $goodsList ) {
		// 整理批量更新商品销量
		$goodsSave = [];
		// 批量更新商品规格：sku销量、库存
		$goodsSpecSave = [];
		foreach ( $goodsList as $goods ) {
			$goodsSave[] = [
				'id'           => $goods['goods_id'],
				'sales_actual' => ['inc', $goods['total_num']]
			];
			$specData    = [
				'goods_spec_id' => $goods['goods_spec_id'],
				'goods_sales'   => ['inc', $goods['total_num']]
			];
			// 付款减库存
			if ( $goods['deduct_stock_type'] === '20' ) {
				$specData['stock_num'] = ['dec', $goods['total_num']];
			}
			$goodsSpecSave[] = $specData;
		}
		// 更新商品总销量
		$this->allowField( true )->isUpdate( true )->saveAll( $goodsSave );
		// 更新商品规格库存
		( new ShopGoodsSpec() )->isUpdate( true )->saveAll( $goodsSpecSave );
	}

}
